﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.SECURITY.MODEL;

namespace KPIS.GERP.SECURITY.DAL
{
    public class SysMenuItemListDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SysMenuItemListDAL()
        {
        }
        public DataSet LoadMenuByUser(int UserSequence)
        {        
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;

            try
            {
           
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@UserSequence", UserSequence);
                dbManager.AddParameters(2, "@SystemID", "ADMIN");

                strSQL = "SELECT DISTINCT ml.MENU_ITEM_SEQ"
                        + ", CONCAT('<div class=font-menu>', ml.MENU_ITEM_NAME, '</div>') AS MENU_ITEM_NAME"
                        + ", ml.UPPER_MENU_ITEM_SEQ"
                        + ", ml.MENU_ITEM_PATH"
                        + ", ml.MENU_ITEM_TARGET"
                        + ", ml.RECORD_STATUS"
                    + " FROM SYS_SC_USERS u"
                    + " INNER JOIN SYS_SC_USER_ROLES ur ON u.USER_SEQ = ur.USER_SEQ"
                        + " INNER JOIN SYS_SC_ROLES r ON ur.ROLE_SEQ = r.ROLE_SEQ"
                        + " INNER JOIN SYS_SC_PERMISSIONS p ON r.ROLE_SEQ = p.ROLE_SEQ"
                        + " INNER JOIN SYS_MENU_ITEMS_LIST ml ON p.MENU_ITEM_SEQ = ml.MENU_ITEM_SEQ"
                    + " WHERE u.RECORD_STATUS <> @DeleteRecord"
                        + " AND ur.RECORD_STATUS <> @DeleteRecord"
                        + " AND r.RECORD_STATUS <> @DeleteRecord"
                        + " AND p.RECORD_STATUS <> @DeleteRecord"
                        + " AND ml.RECORD_STATUS <> @DeleteRecord"
                        + " AND u.USER_SEQ = @UserSequence"
                        + " AND ml.SYSTEM_ID = @SystemID"
                    + " ORDER BY ml.MENU_ITEM_SEQ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
           
            
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
             //   dbManager.Close(); 
              dbManager.Dispose();
            }

            return ds;
        }
        public IList<SysMenuItemListInfo> LoadMenuByUsername(int username)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysMenuItemListInfo> infoList = new List<SysMenuItemListInfo>();
            try
            {

                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@username", username);
                dbManager.AddParameters(2, "@SystemID", "ADMIN");

                strSQL = "SELECT DISTINCT ml.MENU_ITEM_SEQ"
                        + ", CONCAT('<div class=font-menu>', ml.MENU_ITEM_NAME, '</div>') AS MENU_ITEM_NAME"
                        + ", ml.UPPER_MENU_ITEM_SEQ"
                        + ", ml.MENU_ITEM_PATH"
                        + ", ml.MENU_ITEM_TARGET"
                        + ", ml.RECORD_STATUS"
                    + " FROM SYS_SC_USERS u"
                    + " INNER JOIN SYS_SC_USER_ROLES ur ON u.USER_SEQ = ur.USER_SEQ"
                        + " INNER JOIN SYS_SC_ROLES r ON ur.ROLE_SEQ = r.ROLE_SEQ"
                        + " INNER JOIN SYS_SC_PERMISSIONS p ON r.ROLE_SEQ = p.ROLE_SEQ"
                        + " INNER JOIN SYS_MENU_ITEMS_LIST ml ON p.MENU_ITEM_SEQ = ml.MENU_ITEM_SEQ"
                    + " WHERE u.RECORD_STATUS <> @DeleteRecord"
                        + " AND ur.RECORD_STATUS <> @DeleteRecord"
                        + " AND r.RECORD_STATUS <> @DeleteRecord"
                        + " AND p.RECORD_STATUS <> @DeleteRecord"
                        + " AND ml.RECORD_STATUS <> @DeleteRecord"
                        + " AND u.USER_SEQ = @username"
                        + " AND ml.SYSTEM_ID = @SystemID"
                    + " ORDER BY ml.MENU_ITEM_CODE";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysMenuItemListInfo info = new SysMenuItemListInfo();
                        info.Seq = Convert.ToInt32(dr[i]["MENU_ITEM_SEQ"]);
                        info.Name = (dr[i]["MENU_ITEM_NAME"]).ToString();
                        info.UpperSeq = (Convert.IsDBNull(dr[i]["UPPER_MENU_ITEM_SEQ"]) ? null : (Nullable<int>)(dr[i]["UPPER_MENU_ITEM_SEQ"]));
                        info.Path = (dr[i]["MENU_ITEM_PATH"]).ToString();
                        info.Target = (dr[i]["MENU_ITEM_TARGET"]).ToString();
                        info.RecordStatus = (dr[i]["RECORD_STATUS"]).ToString();

                        infoList.Add(info);
                    }

                }

            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                //   dbManager.Close(); 
                dbManager.Dispose();
            }

            return infoList;
        }
        public string GetTitlePage(string pageName)
        {
            string resultTitlePage = "";
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);
                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@PageName", pageName);
                string strSQL = @"SELECT menu_item_name, system_id FROM sys_menu_items_list
                                    WHERE LOWER(menu_item_path) LIKE CONCAT('%',LOWER(@PageName),'%')
                                        and record_status <> @DeleteRecord ";
                DataTable resultDt = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (resultDt.Rows.Count > 0)
                {
                    resultTitlePage = Convert.ToString(resultDt.Rows[0]["menu_item_name"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return resultTitlePage;
        }
    }
}
